package cn.jlearn.proxy.web.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import cn.jlearn.proxy.web.convert.MemberConvert;
import cn.jlearn.proxy.web.model.Member;
import cn.jlearn.proxy.web.pojo.R;
import cn.jlearn.proxy.web.service.MemberService;
import cn.jlearn.proxy.web.vo.MemberVO;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author qianyingchao
 * @version 1.0
 * @date 2021/7/15 1:13 下午
 */
@Controller
@RequestMapping(value = "/admin")
public class LoginController {

    @Resource
    private MemberService memberService;

    @Resource
    private MemberConvert memberConvert;

    @GetMapping(value = "/login.html")
    public ModelAndView login() {
        return new ModelAndView("login/index");
    }

    @ResponseBody
    @PostMapping(value = "/login/action")
    public R<MemberVO> loginAction(@RequestBody JSONObject params) {
        if (StrUtil.isBlank(params.getString("email"))) {
            return R.fail("416", "邮箱地址不能为空");
        }
        if (StrUtil.isBlank(params.getString("password"))) {
            return R.fail("416", "密码不能为空");
        }
        R<Member> r = memberService.login(params.getString("email"), params.getString("password"),
                Optional.ofNullable(params.getBoolean("remember")).orElse(false));
        if (r.isSuccess()) {
            MemberVO memberVO = memberConvert.toMemberVO(r.getData());
            memberVO.setToken(StpUtil.getTokenValue());
            return R.success(memberVO);
        }
        return R.fail(r.getCode(), r.getMessage());
    }

    @ResponseBody
    @DeleteMapping(value = "/logout/action")
    public R<?> logoutAction() {
        StpUtil.logout();
        return R.success();
    }

    @GetMapping(value = "/register.html")
    public ModelAndView register() {
        return new ModelAndView("login/register");
    }

    @ResponseBody
    @PostMapping(value = "/register/action")
    public R<MemberVO> registerAction(@RequestBody JSONObject params) {
        if (StrUtil.isBlank(params.getString("email"))) {
            return R.fail("416", "邮箱地址不能为空");
        }
        if (StrUtil.isBlank(params.getString("password"))) {
            return R.fail("416", "密码不能为空");
        }
        String check = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(params.getString("email"));
        boolean isMatched = matcher.matches();
        if (!isMatched) {
            return R.fail("416", "不是合法邮箱地址");
        }

        R<Member> r = memberService.register(params.getString("email"), params.getString("password"));
        if (r.isSuccess()) {
            return R.success(memberConvert.toMemberVO(r.getData()));
        }
        return R.fail(r.getCode(), r.getMessage());
    }

}
